import request from '../../../../util/request.js'
import cheerio  from 'cheerio'
import iconv from 'iconv-lite'
import fs  from 'fs'
import path, { resolve }  from 'path'
import { fileURLToPath } from 'url'
import { query } from 'express'
import { addSql, getResult, listSql, putSql } from '../../../../util/operateSql.js'
import { sleep } from '../../../../util/common.js'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

const savePath = path.resolve(__dirname,'../../../../cacheTxt/spider/')
const saveFileName = 'spider.txt'

// 排序方式 allvisit,weekvisit,monthvisit 总点击量 周点击量 月点击量
const resolvePath = (baseUrl,params) => {
	return baseUrl+params
}
export const get_web_info = function (baseUrl,params,callback) {
  let urlParams = '',cusUrl = baseUrl + `?`, result='';
  try {
    
    for (const key in params) {
      if (Object.hasOwnProperty.call(params, key)) {
        
        if (params && key && params[key] != 'null') {
          urlParams+= `${key}=${params[key]}&`
        }
      }
    }
    cusUrl += urlParams.substring(0,urlParams.length-1)
    
  
    if (!baseUrl) return callback({code: 200, err: 'no url'})
    const options = {
      url: cusUrl || baseUrl,
      method: 'get',
      responseType: 'arraybuffer'
    }
  
    request(options).then((body) => {
      result = iconv.decode(body, 'utf-8')
			if (!fs.existsSync(savePath)) {
				fs.mkdirSync(savePath,{recursive: true})
			}
      fs.writeFileSync(path.resolve(savePath,saveFileName),result,{encoding: 'utf-8'},err => {
        if (err) throw err
      })
      
        const $ = cheerio.load(result, {decodeEntities: false})
        const parseBody = $(".md-sidebar.md-sidebar--primary .md-nav.md-nav--primary.md-nav--lifted .md-nav__list")
  
        const listTitle = Array.prototype.map.call(parseBody.find('.md-nav__item.md-nav__item--nested').children('nav').children('ul').children('li').children('a'), (e,index) => {
          return {
            title: e.children[0].data,
            link: resolvePath(baseUrl,e.attribs.href) ,
						parent: $(e).parent().parent().parent().attr('aria-label')
						// parent: e.parent().parent().parent().attr('aria-label')
          }
        })

				console.log('listTitle: ', listTitle);
				listTitle.forEach(async (item,index) => {
					setTimeout(() => {
						getBookDetail(listTitle,index + 1)
					}, index * 500);
				})

       callback({code: 0, data: listTitle})
    }).catch(err => {
      callback({code: 500, msg: err+'访问失败: ' +options.title +' : '+ options.url})
    })
  } catch (error) {
    callback({code:500, err})
    return err
  }
}
function extractImageLinks(text) {
	// 正则表达式匹配http或https开头，后面跟着任意字符（非贪婪匹配），然后是.jpg、.png、.gif等图片扩展名
	var imgRegex = /https?:\/\/[\w\-\.\/]+(\.(jpg|jpeg|png|gif))/gi;
	
	// 使用match方法找到所有匹配项
	var imgMatches = text.match(imgRegex);
	
	return imgMatches ? imgMatches : []; // 如果有匹配项则返回，没有则返回空数组
}

export const getBookDetail = (listMenus,index) => {
  try{
    return new Promise((resolve, reject) => {
      if (listMenus.length > 0 && index) {
        const url = listMenus[+index - 1].link || ''
        const title = listMenus[+index - 1].title || ''
				const category = listMenus[+index - 1].parent || ''

        request({url,method: 'get',responseType: 'arraybuffer'}).then(async (contentbody) => {
          const contents = iconv.decode(contentbody, 'utf-8')
          const $ = cheerio.load(contents, {decodeEntities: false})
          const parseBody = $('.md-content')
  
          let res = $(parseBody).html()
					// const sql = addSql('t_spu',{title,category_id: 99,saleable:1,valid:1,merchant_id:98})
					res = res.replace('src="..',`src="${url.split('/').splice(0,6).join('/')}`)
					const img_url = extractImageLinks(res).join()
					const sql = putSql('resource.howtocook',{category,title,img_url,rich_text: res},{title})
					await getResult(sql)
					console.log(title,'新增成功');
					await sleep(1000)

          resolve ({code:0, data: res})
          
        }).catch(err => {
					console.log('err: ', err);
          resolve ({code: 500,data: `访问文章详情失败：${err}. 链接: ${url}` })
        })
      } else {
        resolve ({code: 0, data: {
          listMenus
        }})
      }
    })
  } catch (error) {
    if (listMenus[+catalog - 1]?.link) {
      resolve ({code: 500, data: '查找内容失败: '+  listMenus[+query.catalog - 1]?.link })
    } else {
      resolve ({code: 500, data: '暂无更新:' + err})
    }
    
  }
}


/**
 * 
 * @param {object} query  url:小说链接,catalog章节
 * @param {function} callback 
 */
export const getInfoByBook = (query,callback) => {
  // 章节
  const catalog = query.catalog || null
  const param = {
    url: query.url,
    method: 'get',
    responseType: 'arraybuffer'
  }
  let result = ''
  try {
    
    // 访问文章简介
    request(param).then((body) => {
      result = iconv.decode(body, 'gbk')
      const $ = cheerio.load(result, {decodeEntities: false})
      const articeInfo = $('.divbox.cf')
      const articleLink = articeInfo.find('.btnlink.b_hot.mbs').attr('href')
      const articleContent = articeInfo.find('.tabcontent .tabvalue').children().text()

      // 访问文章章节列表
      request({url:articleLink,method: 'get',responseType: 'arraybuffer'}).then(async (content) => {
        const contents = iconv.decode(content, 'gbk')
        const $ = cheerio.load(contents, {decodeEntities: false})
        const parseBody = $('.chapters')
        const listMenus = Array.prototype.map.call(parseBody.find('.chapter a'),(e) => {
          return {
            title: $(e).text(),
            link: e.attribs.href,
          }
        })
        
        const row = await getBookDetail(listMenus,catalog)
        callback(row)
        
      }).catch((err) => {
        callback({code: 500,data: '访问章节列表失败：' + param.url})
      });
    }).catch(err => {
      callback({code: 500,data: '访问书籍首页失败：' + param.url})
    })
  }catch (error) {
    callback({code: 500,data: '访问失败：' + param.url})
  }
}

const handleRichTextPic = () => {
	const sql = `select * from t_spu where category_id > 80`
	const rows = getResult(sql)
}


export default get_web_info